<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="82px">
      <el-form-item label="排课学期" prop="semester">
        <el-input
          v-model="queryParams.semester"
          placeholder="请输入排课学期"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="起始周" prop="startWeek">
        <el-input
          v-model="queryParams.startWeek"
          placeholder="请输入起始周"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="结束周" prop="endWeek">
        <el-input
          v-model="queryParams.endWeek"
          placeholder="请输入结束周"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="星期" prop="day">
        <el-select v-model="queryParams.day" placeholder="请选择星期" clearable>
          <el-option
            v-for="dict in dict.type.jwxt_day"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="节次" prop="session">
        <el-select v-model="queryParams.session" placeholder="请选择节次" clearable>
          <el-option
            v-for="dict in dict.type.jwxt_session"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="实验室编号" prop="labId">
        <el-input
          v-model="queryParams.labId"
          placeholder="请输入实验室编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课程名" prop="courseName">
        <el-input
          v-model="queryParams.courseName"
          placeholder="请输入课程名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任课教师" prop="teacherName">
        <el-input
          v-model="queryParams.teacherName"
          placeholder="请输入任课教师"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学生班级" prop="studentClass">
        <el-input
          v-model="queryParams.studentClass"
          placeholder="请输入学生班级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['jwxt:scheduling:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['jwxt:scheduling:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['jwxt:scheduling:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['jwxt:scheduling:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="schedulingList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="课表编号" align="center" prop="scheduleId" />
      <el-table-column label="排课学期" align="center" prop="semester" />
      <el-table-column label="起始周" align="center" prop="startWeek" />
      <el-table-column label="结束周" align="center" prop="endWeek" />
      <el-table-column label="星期" align="center" prop="day">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.jwxt_day" :value="scope.row.day"/>
        </template>
      </el-table-column>
      <el-table-column label="节次" align="center" prop="session">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.jwxt_session" :value="scope.row.session"/>
        </template>
      </el-table-column>
      <el-table-column label="实验室编号" align="center" prop="labId" />
      <el-table-column label="课程名" align="center" prop="courseName" />
      <el-table-column label="任课教师" align="center" prop="teacherName" />
      <el-table-column label="学生班级" align="center" prop="studentClass" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['jwxt:scheduling:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['jwxt:scheduling:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改排课管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-form-item label="排课学期" prop="semester">
<!--          <el-input v-model="form.semester" placeholder="请输入排课学期" />-->
          <el-select v-model="form.semester" filterable placeholder="请选择学期">
            <el-option
              v-for="item in this.semesterOption"
              :key="item.id"
              :label="item.semesterName"
              :value="item.semesterName">
            </el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="起始周" prop="startWeek">
          <el-input v-model="form.startWeek" placeholder="请输入起始周" />
        </el-form-item>
        <el-form-item label="结束周" prop="endWeek">
          <el-input v-model="form.endWeek" placeholder="请输入结束周" />
        </el-form-item>-->
        <el-form-item label="周次">
          <el-slider
            v-model="weekRange"
            range
            :min="1"
            :max="20">
          </el-slider>
        </el-form-item>
        <el-form-item label="星期" prop="day">
          <el-select v-model="form.day" placeholder="请选择星期">
            <el-option
              v-for="dict in dict.type.jwxt_day"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="节次" prop="session">
          <el-select v-model="form.session" placeholder="请选择节次">
            <el-option
              v-for="dict in dict.type.jwxt_session"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="实验室编号" prop="labId">
          <el-input v-model="form.labId" placeholder="请输入实验室编号" />
        </el-form-item>
        <el-form-item label="课程名" prop="courseName">
          <el-input v-model="form.courseName" placeholder="非必填" />
        </el-form-item>
        <el-form-item label="任课教师" prop="teacherName">
          <el-input v-model="form.teacherName" placeholder="非必填" />
        </el-form-item>
        <el-form-item label="学生班级" prop="studentClass">
          <el-input v-model="form.studentClass" placeholder="非必填" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listScheduling, getScheduling, delScheduling, addScheduling, updateScheduling,getSemesterList,getCurSemester } from "@/api/jwxt/scheduling";

export default {
  name: "Scheduling",
  dicts: ['jwxt_day', 'jwxt_session'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 排课管理表格数据
      schedulingList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 周次范围
      weekRange: [1,20],
      // 学期动态下拉框数据
      semesterOption:{},
      // 当前学期
      curSemester:null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        semester: null,
        startWeek: null,
        endWeek: null,
        day: null,
        session: null,
        labId: null,
        courseName: null,
        teacherName: null,
        studentClass: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        semester: [
          { required: true, message: "排课学期不能为空", trigger: "blur" }
        ],
        startWeek: [
          { required: true, message: "起始周不能为空", trigger: "blur" }
        ],
        endWeek: [
          { required: true, message: "结束周不能为空", trigger: "blur" }
        ],
        day: [
          { required: true, message: "星期不能为空", trigger: "change" }
        ],
        session: [
          { required: true, message: "节次不能为空", trigger: "change" }
        ],
        labId: [
          { required: true, message: "实验室编号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.fillSemesterOption();
    this.setCurSemester();
  },
  methods: {
    /** 查询排课管理列表 */
    getList() {
      this.loading = true;
      listScheduling(this.queryParams).then(response => {
        this.schedulingList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        scheduleId: null,
        semester: null,
        startWeek: null,
        endWeek: null,
        day: null,
        session: null,
        labId: null,
        courseName: null,
        teacherName: null,
        studentClass: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.scheduleId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.setCurSemester();
      this.open = true;
      this.title = "直接添加排课（不推荐）";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const scheduleId = row.scheduleId || this.ids
      getScheduling(scheduleId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改排课管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.scheduleId != null) {
            updateScheduling(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addScheduling(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const scheduleIds = row.scheduleId || this.ids;
      this.$modal.confirm('是否确认删除排课管理编号为"' + scheduleIds + '"的数据项？').then(function() {
        return delScheduling(scheduleIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('jwxt/scheduling/export', {
        ...this.queryParams
      }, `scheduling_${new Date().getTime()}.xlsx`)
    },
    /** 填充学期动态下拉框数据 */
    fillSemesterOption(){
      getSemesterList().then(respon=>{
        this.semesterOption=respon.data;
      })
    },
    /** 获取当前默认学期 */
    setCurSemester(){
      getCurSemester().then(respon=>{
        this.form.semester=respon.msg;
      })
    }
  },
  watch:{
    weekRange(newValue){
      this.form.startWeek=newValue[0];
      this.form.endWeek=newValue[1];
    },
    form: {
      //深度监听对象内部属性的变化
      deep: true,
      handler(newVal) {
        this.weekRange = [newVal.startWeek, newVal.endWeek];
      }
    }
  }
};
</script>
